package com.yiqi.liebang.feature.mine.model;

import android.text.TextUtils;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yiqi.liebang.common.util.RxClipboardTool;
import com.yiqi.liebang.entity.bo.AllAuthBo;
import com.yiqi.liebang.entity.bo.EducationBo;
import com.yiqi.liebang.entity.bo.UserCardBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.entity.bo.UserParamBo;
import com.yiqi.liebang.entity.bo.WorkBo;
import com.yiqi.liebang.feature.mine.contract.CertificationContract;
import com.yiqi.liebang.feature.mine.contract.UserContract;
import com.yiqi.liebang.feature.mine.dependencies.certification.DaggerCertificationModelComponent;
import com.yiqi.liebang.feature.mine.dependencies.login.DaggerUserModelComponent;
import com.yiqi.liebang.framework.api.ApiLib;
import com.yiqi.liebang.framework.api.UserApi;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class CertificationModel implements CertificationContract.Model {

    @Inject
    UserApi api;


    @Inject
    public CertificationModel() {
        DaggerCertificationModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<UserCardBo> getUserCardData() {
        return api.getUserVisitingCard().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> userCertification(String userHead,
                                                String userName,
                                                String userLabelId, String userIndustry, String userWorkAddress,
                                                String userDetailAddress, String userPhone, String userEmail, String wxId,
                                                String cardNumber,
                                                String cardImageUrl) {

        MultipartBody.Part headFilePart = null;
        MultipartBody.Part cardFilePart = null;
        if (!TextUtils.isEmpty(userHead)) {
            File headFile = new File(userHead);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("userHead", headFile.getName(), requestFile);
        }

        if (!TextUtils.isEmpty(cardImageUrl)) {

            File certificationFile = new File(cardImageUrl);
            RequestBody comanyRequestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), certificationFile);
            cardFilePart = MultipartBody.Part.createFormData("userCardUrl", certificationFile.getName(), comanyRequestFile);
        }


        RequestBody userNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), userName);
        RequestBody userLabelIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), userLabelId);
        RequestBody userIndustryBody = RequestBody.create(MediaType.parse("multipart/form-data"), userIndustry);
        RequestBody userWorkAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), userWorkAddress);
        RequestBody userDetailAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), userDetailAddress);
        RequestBody userPhoneBody = RequestBody.create(MediaType.parse("multipart/form-data"), userPhone);
        RequestBody userEmailBody = RequestBody.create(MediaType.parse("multipart/form-data"), userEmail);
        RequestBody userDetailwXBody = RequestBody.create(MediaType.parse("multipart/form-data"), wxId);
        RequestBody userCardBody = RequestBody.create(MediaType.parse("multipart/form-data"), cardNumber);

        return api.editUserCertification(headFilePart, userNameBody,
                userLabelIdBody, userIndustryBody, userWorkAddressBody, userDetailAddressBody,
                userPhoneBody, userEmailBody, userDetailwXBody, userCardBody, cardFilePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());

    }

    @Override
    public Observable<WorkBo> userGetWork(WorkBo workBo) {
        return api.getWork(workBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<EducationBo> userGetEducation(EducationBo workBo) {
        return api.getEducation(workBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> authWork(String id, String certificateImage, String licenseImage, String workCardImage, String visitingImage) {
        RequestBody idBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);

        MultipartBody.Part certificateFilePart = null;
        MultipartBody.Part licenseFilePart = null;
        MultipartBody.Part workCardFilePart = null;
        MultipartBody.Part visitingFilePart = null;

        if (!TextUtils.isEmpty(certificateImage)) {
            File certificateFile = new File(certificateImage);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), certificateFile);
            certificateFilePart = MultipartBody.Part.createFormData("certificateImage", certificateFile.getName(), requestFile);
        }

        if (!TextUtils.isEmpty(licenseImage)) {
            File licenseFile = new File(licenseImage);
            RequestBody licenseFileBody = RequestBody.create(MediaType.parse("application/otcet-stream"), licenseFile);
            licenseFilePart = MultipartBody.Part.createFormData("licenseImage", licenseFile.getName(), licenseFileBody);
        }


        if (!TextUtils.isEmpty(workCardImage)) {
            File workCardFile = new File(workCardImage);
            RequestBody workCardBody = RequestBody.create(MediaType.parse("application/otcet-stream"), workCardFile);
            workCardFilePart = MultipartBody.Part.createFormData("workCardImage", workCardFile.getName(), workCardBody);
        }

        if (!TextUtils.isEmpty(visitingImage)) {
            File visitingFile = new File(visitingImage);
            RequestBody visitingFileBody = RequestBody.create(MediaType.parse("application/otcet-stream"), visitingFile);
            visitingFilePart = MultipartBody.Part.createFormData("visitingImage", visitingFile.getName(), visitingFileBody);
        }

        return api.authWork(idBody, certificateFilePart, licenseFilePart, workCardFilePart, visitingFilePart)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> authEdu(String id, String DiplomaImage, String degreeImage) {
        RequestBody idBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);

        MultipartBody.Part certificateFilePart = null;
        MultipartBody.Part licenseFilePart = null;


        if (!TextUtils.isEmpty(DiplomaImage)) {
            File certificateFile = new File(DiplomaImage);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), certificateFile);
            certificateFilePart = MultipartBody.Part.createFormData("DiplomaImage", certificateFile.getName(), requestFile);
        }

        if (!TextUtils.isEmpty(degreeImage)) {
            File licenseFile = new File(degreeImage);
            RequestBody licenseFileBody = RequestBody.create(MediaType.parse("application/otcet-stream"), licenseFile);
            licenseFilePart = MultipartBody.Part.createFormData("degreeImage", licenseFile.getName(), licenseFileBody);
        }


        return api.authEducation(idBody, certificateFilePart, licenseFilePart)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<List<WorkBo>> getWorkList() {
        return api.getWorkList().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<List<EducationBo>> getEduList() {
        return api.getEduList().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> uploadAuthenticationImage(String authenticationId, String uploadType, String image) {
        RequestBody authenticationIdBody = null;
        if (!TextUtils.isEmpty(authenticationId)) {
            authenticationIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), authenticationId);
        }

        RequestBody uploadTypeBody = RequestBody.create(MediaType.parse("multipart/form-data"), uploadType);

        MultipartBody.Part imageFilePart = null;

        if (!TextUtils.isEmpty(image)) {
            File certificateFile = new File(image);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), certificateFile);
            imageFilePart = MultipartBody.Part.createFormData("image", certificateFile.getName(), requestFile);
        }

        return api.uploadAuthenticationImage(authenticationIdBody, uploadTypeBody, imageFilePart)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> uploadBasicInfo(String userHead, String userName, String userLabelId, String userIndustry, String userWorkAddress,
                                              String userDetailAddress, String userPhone, String userEmail, String wxId, String cardNumber,
                                              String uploadType, String image) {


        MultipartBody.Part headFilePart = null;
        MultipartBody.Part cardFilePart = null;
        if (!TextUtils.isEmpty(userHead)) {
            File headFile = new File(userHead);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("userHead", headFile.getName(), requestFile);
        }


        RequestBody userNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), userName);
        RequestBody userLabelIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), userLabelId);
        RequestBody userIndustryBody = RequestBody.create(MediaType.parse("multipart/form-data"), userIndustry);
        RequestBody userWorkAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), userWorkAddress);
        RequestBody userDetailAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), userDetailAddress);
        RequestBody userPhoneBody = RequestBody.create(MediaType.parse("multipart/form-data"), userPhone);
        RequestBody userEmailBody = RequestBody.create(MediaType.parse("multipart/form-data"), userEmail);
        RequestBody userDetailwXBody = RequestBody.create(MediaType.parse("multipart/form-data"), wxId);
        RequestBody userCardBody = RequestBody.create(MediaType.parse("multipart/form-data"), cardNumber);
        RequestBody uploadTypeBody = RequestBody.create(MediaType.parse("multipart/form-data"), uploadType);


        if (!TextUtils.isEmpty(image)) {
            File certificationFile = new File(image);
            RequestBody comanyRequestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), certificationFile);
            cardFilePart = MultipartBody.Part.createFormData("image", certificationFile.getName(), comanyRequestFile);
        }

        return api.uplaodBasicInfo(headFilePart, userNameBody,
                userLabelIdBody, userIndustryBody, userWorkAddressBody, userDetailAddressBody,
                userPhoneBody, userEmailBody, userDetailwXBody, userCardBody,uploadTypeBody, cardFilePart).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<UserCardBo> getUserSaveData() {
        return api.getBasicAuthentication().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> toPersonalAuthentication(AllAuthBo allAuthBo) {
        return api.toPersonalAuthentication(allAuthBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}
